<?php
//
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
cfc('core')->checklogin();
$credit_types=credit_types(true);
$return['onemerchid']=$_SESSION['onemerchid'];
if($_RQ['op']=='main'){
	$goodsid = intval($_RQ['goodsid']);
	$optionsid = intval($_RQ['optionsid']);
	$return['setting']=$this->setting;
	//下单页
	$item=array();
	$return['post'] = array();
	if($goodsid){
		$return['post']['goodsid'] = $goodsid;
		$goods=pdo_get('jy_creditshop_goods',array('id'=>$goodsid));
		if(!$goods){
			exi('指定商品不存在或已删除！','error','goback');
		}
		model('jy_creditshop/order')->check_times($goods);
		model('jy_creditshop/order')->check_buy($goods,true);
		if($optionsid){
			$options = pdo_get('jy_creditshop_option',array('goodsid'=>$goodsid,'id'=>$optionsid));
			$goods['optionid']=$options['id'];
			$goods['money']=$options['price'];
			$goods['credit']=$options['credit'];
			$return['post']['optionid'] = $goods['optionid'];
			$specs = explode("_", $options['specs']);
			$specsall = pdo_getall('jy_creditshop_spec_item',array('id'=>$specs,'uniacid'=>$_SESSION['uniacid']),array('id','title'),'','displayorder asc');
			if($specsall){
					$optiontitle='';
					foreach($specsall as $k=>&$sp){
						if($k==0){
							$optiontitle .=$sp['title'];
						}else{
							$optiontitle .='，'.$sp['title'];
						}
					}
			}
			$goods['optiontitle']= $optiontitle;
		}else{
			$goods['optionid']=0;
			$return['post']['optionid'] = 0;
		}
		$return['post']['total'] = $goods['total'];
		$order['list']=array();
		$order['list'][]=$goods;
		$goods['storeids']=$goods['storeids']?json_decode($goods['storeids'],true):array();
		if(count($goods['storeids'])){
			$carrier_list=array();
			foreach($goods['storeids'] as $sv){
				$carrier_list[] = pdo_get('jy_weishop_store',array('uniacid' =>$_SESSION['uniacid'],'id'=>$sv['id']));
			}	
		}else{
			$carrier_list = pdo_getall('jy_weishop_store',array('uniacid' =>$_SESSION['uniacid'],'status'=>1,'type'=>array(1,3)),'','','paixu desc,id desc');
		}
		if($carrier_list){
				foreach($carrier_list as &$pv){
					$pv['pcd']=json_decode($pv['pcd'],true);
				}
		}
		$goods['stores']=$carrier_list;
		$return['selectstore']=$carrier_list['0'];
	}
	//计算运费
	$item['address']=array();
	$order['dispatchprice']=0;
	if($goods['goodstype']==0 && !$goods['isverify']){
		if(intval($_RQ['addressid'])){
			$item['address']=pdo_get('core_address',array('mid'=>$_SESSION['uid'],'id'=>intval($_RQ['addressid'])));
			$item['addressid']=intval($_RQ['addressid']);
		}else{
			$member_address=pdo_getall('core_address',array('mid'=>$_SESSION['uid']));
			if($member_address){
				foreach($member_address as &$address){
					if($address['isdefault']){
						$item['addressid']=$address['id'];
						$item['address']=$address;
						break;
					}
				}
				if(!$item['addressid']){
					$item['addressid']=$member_address['0']['id'];
					$item['address']=$member_address['0'];
					
				}
				
			}
		}
		//$city_express=plugin('jy_weishop')->plugin_setting('citypeisong');
		if($item['address']){
			$item['address']['pcd']=json_decode($item['address']['pcd'],true);
			try{
				if($city_express['isopen']){
					$addressinfo=$item['address']['pcd']['province'] . $item['address']['pcd']['city'] . $item['address']['pcd']['district'] . $item['address']['pcd']['street'] . $item['address']['address'];
					$get_freight=model('jy_creditshop/order/dispatch')->get_peisong($addressinfo,$_SESSION['store_id'],$item['address']['id'],$goods['money']);
					if($get_freight['is_dispatch']==1){
						$get_freight=model('jy_creditshop/order/dispatch')->get_freight($order['list'],$item['address']['pcd'],true,true);
					}else{
						$order['is_peisong']=1;
						$order['peisong_juli']=price($get_freight['juli']);
						//不在同城,不能下单！
						if($get_freight['iscity']==0){
							$order['outarea']=1;
						}
					}
				}else{
					$get_freight=model('jy_creditshop/order/dispatch')->get_freight($order['list'],$item['address']['pcd'],true,true);
					if($get_freight['outarea']){
							$order['outarea']=1;
					}
				}
				$order['dispatchprice']=$get_freight['fee'];
				$order['isonebaoyou']=$get_freight['isonebaoyou'];
				$order['addbaoyoufee']=$get_freight['addbaoyoufee'];
			}catch(Exception $e){
				unset($item['address'],$item['addressid']);
				$order['dispatchprice']=0;
				$item['isnoarea']=1;
			}
		}else{
			if($city_express['isopen']){
					$get_freight=model('jy_creditshop/order/dispatch')->get_peisong('',$_SESSION['store_id'],'',$goods['money']);
					$order['is_peisong']=1;
					$order['peisong_juli']=price($get_freight['juli']);
			}else{
				$get_freight=model('jy_creditshop/order/dispatch')->get_freight($order['list'],'',true,true);
			}
			$order['dispatchprice']=$get_freight['fee'];
			$order['isonebaoyou']=$get_freight['isonebaoyou'];
			$order['addbaoyoufee']=$get_freight['addbaoyoufee'];
		}
	}
	$goods['money']=intval($goods['money']);
	$goods['dispatch']=intval($goods['dispatch']);
	if($goods['credit_type']){
		$goods['credit_typetitle']=$credit_types[$goods['credit_type']];
	}else{
		$goods['credit_type']=1;
		$goods['credit_typetitle']='积分';
	}
	$return['log_listurl'] ='jy_credit4shop/log_list4/';
	$this->session('log_listurl',$return['log_listurl']);
	$return['item'] = $item;
	//提交数据
	$return['post']=array('goodsid'=>$goods['id']);
	$goods['creditmore']=json_decode($goods['creditmore'],true);	
	foreach($goods['creditmore'] as &$gv){
		$gv['credit_title']=$credit_types[$gv['credit_type']];
		$gv['ratefee']=$gv['credit']*$return['setting']['shoufee_rate'.$gv['credit_type']]/100;
	}
	$taocankey=intval($_RQ['taocankey']);
	if($goods['is_moretype']){
		$return['post']['taocan']=$goods['creditmore'][$taocankey];
	}else{
		$return['post']['taocan']=array(
			'credit_title'=>$credit_types[$goods['credit_type']],
			'credit'=>$goods['credit'],
			'money'=>$goods['money'],
			'ratefee'=>$goods['credit']*$return['setting']['shoufee_rate'.$goods['credit_type']]/100,
		);
	}
	$shoufee_man=pdo_getsum('jy_creditshop_log',array('isqingratefee'=>0,'uniacid'=>$_SESSION['uniacid']),'ratefee');
	if($shoufee_man>=$return['setting']['shoufee_man'] && $return['setting']['shoufee_man']){
		$return['post']['taocan']['ratefee']=0;
	}
	$return['post']['optionid']=$optionsid;
	$return['post']['num'] = $_RQ['num']?:1;
	$return['post']['addressid'] = $item['addressid'];
	$return['post']['sendtype'] = $order['sendtype']?:1;
	$return['post']['storeid'] = $return['selectstore']['id'];
	$return['post']['dispatchprice']=$order['dispatchprice'];
	$order['endprice']=round($return['post']['taocan']['money']*$return['post']['num'] + $order['dispatchprice'],2);
	$order['no_dispatch_price']=round($return['post']['taocan']['money'],2);
	$order['endprice']=$order['endprice']>=0?$order['endprice']:0;
	$return['post']['totalprice']=$return['post']['taocan']['money']*$return['post']['num'];
	$return['post']['totalcredit']=$return['post']['taocan']['credit']*$return['post']['num'];
	$return['post']['ratefee']=$return['post']['taocan']['ratefee']*$return['post']['num'];
	$return['post']['endprice']=$order['endprice'];
	$return['post']['endprice']=$return['post']['endprice']>=0?$return['post']['endprice']:0;
	$return['goods'] = $goods;
	$return['order'] = $order;
	//获取行政数据
	$return['province']=cfc('tools')->pcd();
	$_SESSION['order']=$order;
	exi($return);
}
if($_RQ['op']=='pay'){
	$post=get_postdata();
	if($post['addressid']){
		$address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'id'=>$post['addressid']));
	}
	$id=intval($post['goodsid']);
	if(!$id){
		exi('未获取到商品信息！','error');
	}
	$goods=pdo_get('jy_creditshop_goods',array('id'=>$id));
	if($optionsid){
			$options = pdo_get('jy_creditshop_option',array('id'=>$optionsid));
			$goods['optionid']=$options['id'];
			$goods['money']=$options['price'];
			$goods['credit']=$options['credit'];
	}else{
		$goods['money']=$goods['price'];
	}
	$post['num']=$post['num']?:1;
	$money=$goods['money'];
	$credit=$post['taocan']['credit']*$post['num'];
	if($goods['goodstype']==0 && !$goods['isverify']){
		$dispatchprice=intval($post['dispatchprice']);
		$money=$post['taocan']['money']+$dispatchprice;
		if(!$post['addressid']){
			exi('请填写收货地址！','error');
		}
	}
	$basic=$this->setting;
	//判断用户的积分是否够用
	$credit_type=$post['taocan']['credit_type']?:$goods['credit_type'];
	$credit_type=$credit_type?:1;
	if($_SESSION['onemerchid'] && !$basic['noone_credit']){
		$user_credit=pdo_getcolumn('jy_merch_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'credit'.$credit_type);
		$merchname=pdo_getcolumn('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['onemerchid']),'merchname');
	}else{
		$user_credit=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$_SESSION['uid']),'credit'.$credit_type);
		$merchname='本平台';
	}
	if($user_credit<$credit+$post['ratefee']){
		$credit_typetitle=credit_types(true)[$credit_type];
		exi('您在【'.$merchname.'】的'.$credit_typetitle.'不足，提交失败！','error');
	}
	//兑换商品
	$insert=array(
		'uniacid'=>$_SESSION['uniacid'],
		'merchid'=>intval($goods['merchid']),
		'paymerchid'=>$_SESSION['onemerchid'],
		'credit_type'=>$credit_type?:1,
		'mid'=>$_SESSION['uid'],
		'goodsid' => $id,
		'logno' =>get_ordersn('jy_creditshop_log','logno'),
		'type' => $goods['type'],
		'money' => $money,
		'credit' => $credit,
		'taocan' => $post['taocan'],
		'ratefee' => $post['ratefee'],
		'storeid' => $post['storeid'],
		'total' => $post['num']?:1,
		'optionid' => $post['optionid'],
		'addressid' => $post['addressid'],
		'dispatch' => $goods['dispatch'],
		'set_credit1' => intval($goods['set_credit1']),
		'remark' => trim($post['remark']),
	//	'address' => iserializer($address),
		'status' => 0,
		'paystatus' => 0,
		'dispatchstatus' =>0,
		'createtime' => time(),
		'realname' => trim($address['name']),
		'mobile' => trim($address['mobile'])
	);
	$order_id=model('jy_creditshop/order')->create($insert);
	if($money>0){
	//支付
	 $params=array(
			'id'=>$order_id,
			'title'=>'积分商城订单',
			'fee'=>$money,
			//'pay_type'=>'wechat',//可选，此项作用为指定支付方式
			'no_type'=>array('hdfk','credit1','credit3','credit4','credit5','credit6'),//可选，此项作用为指定不允许使用哪种支付方式
			'back_url'=>'jy_credit4shop/create/payresult/',//必填，指定异步支付结果通知地址，采用系统标准URL书写格式
			'success_url'=>$this->session('log_listurl'),
			'error_url'=>$this->session('log_listurl'),
			'plugin'=>'jy_creditshop',
		);
		if($_SESSION['onemerchid']){
			if(!$basic['noone_credit']){
				$params['merchid']=$_SESSION['onemerchid'];
			}
		}
		$payid=cfc('pay')->create_pay_order($params);
		pdo_update('jy_creditshop_log',array('pay_id'=>$payid), array('id' => $order_id));		
		$return['payid']=$payid;
		$return['id']=$order_id;
		exi($return);
	}else{
		$params=array(
			'onemerchid'=>$_SESSION['onemerchid'],
		);
		model('jy_creditshop/order')->order_pay($order_id,$params);
        if($insert['credit_type']==4 && !$insert['type']){
            pdo_update('jy_creditshop_log',array('status'=>7),array('id'=>$order_id));
        }
		exi('兑换成功！','',$this->session('log_listurl'));
	}
	
}

if($_RQ['op']=='payresult'){
	$payresult=$_SESSION['pay_result'];
	if(!$payresult){
		exi('支付失败，未收到支付结果！','error');
	}
	if($payresult['status']!=1){
		exi('支付失败！','error');
	}
	$order=pdo_get('jy_creditshop_log',array('id'=>$payresult['id']));
	if(!$order){
		exi('支付失败！订单不存在！','error');
	}
	$params=array(
		'paytype'=>$payresult['pay_type'],
		'transid'=>$payresult['transaction_id'],
		'onemerchid'=>$payresult['onemerchid'],
	);
	model('jy_creditshop/order')->order_pay($order['id'],$params);
	if($order['credit_type']==4 && !$order['type']){
		pdo_update('jy_creditshop_log',array('status'=>7),array('id'=>$order['id']));
	}
	exi('提交成功!');
}
if($_RQ['op']=='get_freight'){
	$addressid=intval($_RQ['addressid']);
	$addressid=$addressid?:intval($_RQ['address']);
	if(!$addressid){
		exi('请先选择地址！','error');
	}
	$item=pdo_get('core_address',array('id'=>$addressid));	
	if(!$item['pcd']){
		exi('0');
	}
	$item['pcd']=json_decode($item['pcd'],true);
	//$city_express=$this->plugin_setting('citypeisong');
	if($city_express['isopen']){
		$addressinfo=$item['pcd']['province'] . $item['pcd']['city'] . $item['pcd']['district'] . $item['pcd']['street'] . $item['address'];
		$freight=model('jy_creditshop/order/dispatch')->get_peisong($addressinfo,$_SESSION['store_id'],$item['id'],$_SESSION['order']['total_price']);
		if($freight['is_dispatch']==1){
			$freight=model('jy_creditshop/order/dispatch')->get_freight($_SESSION['order']['list'],$item['address']['pcd'],true,true);
		}else{
			//不在同城,不能下单！
			if($freight['iscity']==0){
				$freight['outarea']=1;
			}
		}
	}else{
		$freight=model('jy_creditshop/order/dispatch')->get_freight($_SESSION['order']['list'],$item['pcd'],'',true);
		if($freight['outarea']){
			$freight['outarea']=1;
		}
	}
	if($_RQ['showbaoyou']){
		exi($freight);
	}else{
		exi($freight['fee']);
	}
	
}
?>